package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.Order;
import com.mmlc.meimei.lichengkongjian.classify.ModePaySelectActivity;
import com.mmlc.meimei.lichengkongjian.classify.PayResultActivity;
import com.mmlc.network.filter.GoPayRequestFilter;
import com.mmlc.network.responsebean.GoPayResponseBean;
import com.mmlc.utils.PayUtils;
import com.mmlc.utils.ToastUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/7/19 0019.
 */
public class ActivityShowGoPay extends InterActivity implements View.OnClickListener {


    @InjectView(R.id.iv_photo)
    ImageView ivPhoto;
    @InjectView(R.id.bt_commit)
    TextView btCommit;
    @InjectView(R.id.orderNumber)
    TextView orderNumber;
    @InjectView(R.id.tv_orderPrice)
    TextView tvOrderPrice;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.iv_set)
    ImageView ivSet;
    @InjectView(R.id.rl_mode_pay)
    RelativeLayout rl_mode_pay;
    @InjectView(R.id.tv_pay_content)
    TextView tv_pay_content;
    @Override
    protected int initContentView() {
        return R.layout.activity_showgopay;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        processData();
    }
    boolean flag;
    PayUtils payUtils;
    Order order = new Order();

    private void processData() {

        payUtils = new PayUtils(ActivityShowGoPay.this);

        final Intent intent = getIntent();
        order.setOrder_id(intent.getStringExtra("order_id"));
        order.setOrder_price(intent.getStringExtra("order_price"));
        order.setPay_name(intent.getStringExtra("pay_name"));
        order.setPay_code(intent.getStringExtra("pay_code"));
        order.setTitle(intent.getStringExtra("title"));
        order.setBody(intent.getStringExtra("body"));
        order.setTimestamp(intent.getStringExtra("timestamp"));
        order.setNotify_url(intent.getStringExtra("notify_url"));
        order.setPay_url(intent.getStringExtra("pay_url"));
        order.setExt1(intent.getStringExtra("ext1"));
        order.setGift_score(intent.getStringExtra("gift_score"));
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivSet.setVisibility(View.GONE);
        tvTitle.setText("订单提交");
        btCommit.setOnClickListener(this);

        initUI(intent);
    }

    private void initUI(Intent intent) {
        String order_id = intent.getStringExtra("order_id");
        String order_price = intent.getStringExtra("order_price");
        orderNumber.setText("订单编号:  "+order_id);
        tvOrderPrice.setText("应付金额:  ¥ "+order_price);
        tv_pay_content.setText(order.getPay_name());
        rl_mode_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(activity, ModePaySelectActivity.class).putExtra("type","payagain" ), 808);
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_commit:
                payUtils.toPay(order, new PayUtils.OnPayResultListener() {
                    @Override
                    public void payResult(int payType, int payResult) {
                        if (payResult == 0) {
                            ToastUtils.showToast("支付成功");
                            Intent intent = new Intent(activity, PayResultActivity.class);
                            intent.putExtra("order", order);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtils.showToast("支付失败");
                        }
                    }
                });

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        payUtils.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null && requestCode == 808 && resultCode == 200 ){
            String code = data.getStringExtra("code");
            String name = data.getStringExtra("name");
            tv_pay_content.setText(name);
            goPayDetail(getIntent().getStringExtra("order_id"),code);
            flag = true;
            btCommit.setBackgroundColor(getResources().getColor(R.color.gray_pressed));
        }else {
            payUtils.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void goPayDetail(final String orderId, String code) {
        GoPayRequestFilter filter = new GoPayRequestFilter(activity);
        filter.requestBean.method = "goPay";
        filter.requestBean.order_id = orderId;
        filter.requestBean.pay_code = code;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GoPayResponseBean>() {

            @Override
            public void onSuccess(GoPayResponseBean result) {
                order.setOrder_id(result.getData().getOrder_id());
                order.setOrder_price(result.getData().getOrder_price());
                order.setPay_name(result.getData().getPay_name());
                order.setPay_code(result.getData().getPay_code());
                order.setTitle(result.getData().getTitle());
                order.setBody(result.getData().getBody());
                order.setTimestamp(result.getData().getTimestamp()+"");
                order.setNotify_url(result.getData().getNotify_url());
                order.setPay_url(result.getData().getPay_url());
                order.setGift_score(result.getData().getGift_score());
                btCommit.setBackgroundColor(getResources().getColor(R.color.black_deep));
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

}
